package enemy
{
	import org.flixel.*;
	import utilities.TextBox;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class EvilSpirit extends Unit
	{
		private var textBox:TextBox;
		
		public function EvilSpirit(i:uint, j:uint, useTileNumbers:Boolean = true)
		{
			super(i, j);
			loadGraphic(Asset.ImgEvilSpirit, true);
			
			addAnimation("UP", [0, 1, 2, 1], 6);
			addAnimation("DOWN", [3, 4, 5, 4], 6);
			addAnimation("LEFT", [6, 7, 8, 7], 6);
			addAnimation("RIGHT", [9, 10, 11, 10], 6);
			play("DOWN");
			
			if (useTileNumbers)
			{
				var pos:FlxPoint = Registry.maze.getTileCorner(i, j);
				x = pos.x;
				y = pos.y;
			}
			
			textBox = null;
		}
		
		override public function update():void
		{
			if (pathSpeed == 0)
			{
				goToRandomTile();
			}
			changeAnimation();
			if (Registry.maze.playerIsInSight(getMidpoint(), facing, 5) > 0)
			{
				if (textBox == null || textBox.finished)
				{
					textBox = this.say("AHH RUN AWAY!", 100, 3);
					FlxG.state.add(textBox);
				}
				goToRandomTile(200);
			}
		}
		
		private function changeAnimation():void
		{
			if (velocity.y < 0)
			{
				play("UP");
				facing = UP;
			}
			else if (velocity.y > 0)
			{
				play("DOWN");
				facing = DOWN;
			}
			else if (velocity.x < 0)
			{
				play("LEFT");
				facing = LEFT;
			}
			else if (velocity.x > 0)
			{
				play("RIGHT");
				facing = RIGHT;
			}
		}
		
		private function goToRandomTile(speed:Number = 100):void
		{
			var path:FlxPath = Registry.maze.findPath(this.getMidpoint(), Registry.maze.getRandomPathTileCenter());
			if (path != null)
			{
				followPath(path, speed);
			}
		}
	}
}